package com.qqtech.qquan.circle.service;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.circle.dao.CircleMemberDao;
import com.qqtech.qquan.circle.dao.CircleMemberGradeDao;
import com.qqtech.qquan.circle.enums.CircleConst;
import com.qqtech.qquan.circle.model.CircleMember;
import com.qqtech.qquan.circle.model.CircleMemberGrade;
import com.qqtech.qquan.circle.model.CircleMemberGradeVo;
import com.qqtech.qquan.circle.model.CircleSysDict;

import jersey.repackaged.com.google.common.collect.Lists;

@Service
public class CircleMemberGradeServiceImpl extends BaseServiceImpl<CircleMemberGrade>
		implements CircleMemberGradeService {
	public static final Logger log = LoggerFactory.getLogger(CircleMemberGradeServiceImpl.class);
	
	@Resource
	private CircleMemberGradeDao circleMemberGradeDao;
	@Resource
	private CircleMemberDao circleMemberDao;
	@Resource
	private CircleSysDictService circleSysDictService;

	@Override
	protected BaseDao<CircleMemberGrade> getBaseDao() {
		return circleMemberGradeDao;
	}

	@Override
	public int copyPresetGrade2Circle(Integer circleId) {
		//预设置的成员等列表
		List<CircleSysDict> presetGradeList = circleSysDictService.get(CircleConst.DICTYPE_CIRCLE_MEMBER_GRADE_PRESET);
		List<CircleMemberGrade> gradeList = Lists.newArrayList();
		CircleMemberGrade memberGrade = null;
		if(presetGradeList != null && presetGradeList.size()>0){
			for(CircleSysDict dict : presetGradeList){
				memberGrade = new CircleMemberGrade();
				memberGrade.setCircleId(circleId);
				memberGrade.setIsUsed(YesNoEnum.YES.getCode());
				memberGrade.setGradeCode(Integer.parseInt(dict.getCode()));
				memberGrade.setGradeName(dict.getName());
				gradeList.add(memberGrade);
			}
		}
		if(gradeList!=null && gradeList.size()>0){
			return circleMemberGradeDao.insertInBatch(gradeList);//批量入库
		}
		return 0;
	}

	@Override
	public int setGradeUsed(Integer circleId, Integer gradeCode, boolean isUsed) {
		CircleMemberGrade  memberGrade = new CircleMemberGrade();
		memberGrade.setCircleId(circleId);
		memberGrade.setGradeCode(gradeCode);
		memberGrade.setIsUsed(isUsed ? YesNoEnum.YES.getCode() : YesNoEnum.NO.getCode() );
		return circleMemberGradeDao.updateGradeUsed(memberGrade);
	}

	@Override
	public CircleMemberGradeVo getMemberGrade(Integer circleId, Integer gradeCode) {
		if(circleId==null || gradeCode==null){
			return null;
		}
		CircleMemberGrade  grade4Query = new CircleMemberGrade();
		grade4Query.setCircleId(circleId);
		grade4Query.setGradeCode(gradeCode);
		List<CircleMemberGradeVo> memberGradeList= circleMemberGradeDao.queryList(grade4Query);
		if(memberGradeList!=null && memberGradeList.size()>0){
			return memberGradeList.get(0);
		}else{
			return null;
		}
	}
	
	@Override
	public boolean isHasGrant2Member(Integer circleId, Integer gradeCode) {
		CircleMember member = new CircleMember();
		member.setCircleId(circleId);
		member.setGradeCode(gradeCode);
		return circleMemberDao.queryCount(member) > 0;
	}
}